package com.fsts.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 程序与数据库连接中心
 * 
 * 
 * @version 1.0
 * @since JDK1.6(建议)
 */
public class JDBCUtil {
	// 静态块，在类加载的时候就加载数据库连接驱动
	static {
		String driverName = "com.mysql.jdbc.Driver";
		try {
			Class.forName(driverName);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 得到connection连接
	 * 
	 * @return
	 */
	public static Connection getConn() {
		String url = "jdbc:mysql://127.0.0.1/fsts?useUnicode=true&amp;characterEncoding=utf8"; // 链接数据库的URL，
		String userName = "root"; // 用户名
		String pwd = "root"; // 登陆密码
		Connection con = null; // 接口不能被new，设置为null
		try {
			con = DriverManager.getConnection(url, userName, pwd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}

	/**
	 * 关闭数据库连接
	 * 
	 * @param rs
	 * @param stmt
	 * @param con
	 */
	public static void close(ResultSet rs, Statement stmt, Connection con) {
		try {
			if (rs != null)
				rs.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		try {
			if (stmt != null)
				stmt.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		try {
			if (con != null)
				con.close();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
}
